package cn.com.dcsgo.controller;

import cn.com.dcsgo.ActivityFeignClient;
import cn.com.dcsgo.model.order.CartInfo;
import cn.com.dcsgo.result.Result;
import cn.com.dcsgo.security.AuthContextHolder;
import cn.com.dcsgo.service.CartService;
import cn.com.dcsgo.vo.order.OrderConfirmVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * CartController
 * 使用Redis进行数据的存储
 *
 * @author Dcsgo
 * @since 2025/5/9 10:01
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("weixin/cart")
@RequiredArgsConstructor
public class CartController {
    private final CartService cartService;
    private final ActivityFeignClient activityFeignClient;

    @ApiOperation("添加商品")
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result<Serializable> addItem(@PathVariable Long skuId, @PathVariable Integer skuNum) {
        Long userId = AuthContextHolder.getUserId();
        cartService.addItem(userId, skuId, skuNum);
        return Result.ok(null);
    }

    @ApiOperation("删除一个商品")
    @DeleteMapping("deleteCart/{skuId}")
    public Result<Serializable> removeItem(@PathVariable Long skuId) {
        Long userId = AuthContextHolder.getUserId();
        cartService.removeItem(userId, skuId);
        return Result.ok(null);
    }

    @ApiOperation("批量删除商品")
    @PostMapping("batchDeleteCart")
    public Result<Serializable> removeItem(@RequestBody List<Long> skuIdList) {
        Long userId = AuthContextHolder.getUserId();
        cartService.removeItem(userId, skuIdList);
        return Result.ok(null);
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("deleteAllCart")
    public Result<Serializable> clear() {
        Long userId = AuthContextHolder.getUserId();
        cartService.clear(userId);
        return Result.ok(null);
    }

    @ApiOperation("获取商品列表")
    @GetMapping("cartList")
    public Result<List<CartInfo>> getItems() {
        return Result.ok(cartService.getItems(AuthContextHolder.getUserId()));
    }

    @ApiOperation("获取购物车结算信息")
    @GetMapping("activityCartList")
    public Result<OrderConfirmVo> getOrderConfirmVo() {
        Long userId = AuthContextHolder.getUserId();
        List<CartInfo> items = cartService.getItems(userId);
        return Result.ok(activityFeignClient.getCartSettlementInformation(userId, items));
    }
}
